package com.xuxin.baby.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ActivityUtils;
import com.lxj.xpopup.XPopup;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.UserDataBean;
import com.xuxin.baby.contract.UserContract;
import com.xuxin.baby.popup.ConfirmPopup;
import com.xuxin.baby.presenter.UserPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.BaseUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.11.21 0021
 * 设置页面
 */
public class SettingActivity extends BaseActivity implements UserContract.View {
    private final UserContract.Presenter presenter = new UserPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.setting_phone)
    TextView setting_phone;
    @BindView(R.id.button)
    Button button;

    private ACache aCache;
    private String memberId;//用户id
    private String tenantId;//租户id
    private String token;//授权token

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_setting);
    }

    @Override
    public void initView() {
        title_name.setText("设置");
        button.setText("退出登录");
    }

    @Override
    public void initData() {
        aCache = ACache.get(this);
        memberId = aCache.getAsString(BaseConstant.memberId);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.title_left, R.id.setting_phone_update,
            R.id.setting_about, R.id.button})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.setting_phone_update) {
            ActivityUtils.startActivityForResult(this, SettingPhoneActivity.class, 200);
        } else if (id == R.id.setting_about) {
            ActivityUtils.startActivity(SettingAboutActivity.class);
        } else if (id == R.id.button) {
            new XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .dismissOnBackPressed(true)
                    .asCustom(new ConfirmPopup(context, "确定要退出此账号吗？", new ConfirmPopup.OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            popup.setTitle("正在退出登录，请稍后").show();
                            handler.sendEmptyMessageDelayed(200, 1000);
                        }
                    })).show();
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getUserData(tenantId, token, memberId);
            } else if (msg.what == 200) {
                BaseUtils.loginExit(context);
            }
            return false;
        }
    });


    @Override
    public void onSuccess(UserDataBean userDataBean) {
        if (null != userDataBean && null != userDataBean.getResult()) {
            String phone = userDataBean.getResult().getMobile();
            setting_phone.setText(phone);
        }
    }

    @Override
    public void onImageSuccess(BaseBean baseBean) {

    }

    @Override
    public void onSuccess(BaseBean baseBean) {

    }
}